//
//  SmsCloudPhoneAlertPopUpView.m
#import "SmsCloudPhoneAlertPopUpView.h"
#import "zhPopupController.h"
@interface SmsCloudPhoneAlertPopUpView()
@property (nonatomic, strong) zhPopupController *smsCloudPhoneAlertPopUp;
@property (nonatomic, copy) void (^SmsCloudGoRecorBlock)(void);
@property (nonatomic, strong)SmsCloudCityModel *smsCloudCityMdl;
@property (nonatomic, strong)SmsCloudProductModel *smsCloudProductMdl;
@property (nonatomic, assign)double codeTime;

@property (nonatomic, strong)UILabel * smsCloudPlaContentLab;
@property (nonatomic, strong)UILabel * smsCloudPhoneContentLab;

@property (nonatomic, strong)NSMutableArray * smsCloudRecords;
@end
@implementation SmsCloudPhoneAlertPopUpView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
        self.layer.cornerRadius = [SmsCloudHead SmsCloudScreewRatio:20];
        self.clipsToBounds = true;
        [self smsCloudInitView];
        self.smsCloudRecords = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"SmsCloudRecord"]];
        if(!self.smsCloudRecords){
            self.smsCloudRecords = [[NSMutableArray alloc] init];
        }
    }
    return self;
}
- (void)smsCloudInitView{
    UILabel * smsCloudHeadLab = [[UILabel alloc] init];
    smsCloudHeadLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#222222"];
    smsCloudHeadLab.text = @"获取成功";
    smsCloudHeadLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:36] weight:UIFontWeightRegular];
    [self addSubview:smsCloudHeadLab];
    [smsCloudHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:66]));
        make.centerX.equalTo(self);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:50]));
    }];
    UIView *smsCloudContentView = [[UIView alloc] init];
    smsCloudContentView.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#F5F9FC"];
    smsCloudContentView.layer.cornerRadius = [SmsCloudHead SmsCloudScreewRatio:16];
    smsCloudContentView.clipsToBounds = true;
    [self addSubview:smsCloudContentView];
    [smsCloudContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(smsCloudHeadLab.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:30]);
        make.centerX.equalTo(self);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:642]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:205]));
    }];
    UILabel * smsCloudPlaLab = [[UILabel alloc] init];
    smsCloudPlaLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#262729"];
    smsCloudPlaLab.text = @"注册国家";
    smsCloudPlaLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightRegular];
    [smsCloudContentView addSubview:smsCloudPlaLab];
    [smsCloudPlaLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:38]));
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:39]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:42]));
    }];
    self.smsCloudPlaContentLab = [[UILabel alloc] init];
    self.smsCloudPlaContentLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#262729"];
    self.smsCloudPlaContentLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightRegular];
    [smsCloudContentView addSubview:self.smsCloudPlaContentLab];
    [self.smsCloudPlaContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(smsCloudPlaLab);
        make.left.equalTo(smsCloudPlaLab.mas_right).offset([SmsCloudHead SmsCloudScreewRatio:56]);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:42]));
    }];
    UILabel * smsCloudPhoneLab = [[UILabel alloc] init];
    smsCloudPhoneLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#262729"];
    smsCloudPhoneLab.text = @"手机号";
    smsCloudPhoneLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightRegular];
    [smsCloudContentView addSubview:smsCloudPhoneLab];
    [smsCloudPhoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(smsCloudPlaLab.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:38]);
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:39]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:42]));
    }];
    self.smsCloudPhoneContentLab = [[UILabel alloc] init];
    self.smsCloudPhoneContentLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#262729"];
    self.smsCloudPhoneContentLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightRegular];
    [smsCloudContentView addSubview:self.smsCloudPhoneContentLab];
    [self.smsCloudPhoneContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(smsCloudPhoneLab);
        make.left.equalTo(smsCloudPhoneLab.mas_right).offset([SmsCloudHead SmsCloudScreewRatio:56]);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:42]));
    }];
    UIButton * smsCloudPhoneCopyBtn = [[UIButton alloc] init];
    [smsCloudPhoneCopyBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudPhoneCopy"] forState:UIControlStateNormal];
    [smsCloudPhoneCopyBtn addTarget:self action:@selector(smsCloudPhoneCopy) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:smsCloudPhoneCopyBtn];
    [smsCloudPhoneCopyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:72]));
        make.centerY.equalTo(self.smsCloudPhoneContentLab);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:102]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:58]));
    }];
    
    UIImageView* smsCloudPhoneAlertImgView = [[UIImageView alloc] init];
    smsCloudPhoneAlertImgView.userInteractionEnabled = YES;
    smsCloudPhoneAlertImgView.image = [SmsCloudHead SmsCloudImageNamed:@"smsCloudPhoneAlert"];
    smsCloudPhoneAlertImgView.contentMode = UIViewContentModeScaleToFill;
    [self addSubview:smsCloudPhoneAlertImgView];
    [smsCloudPhoneAlertImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(smsCloudContentView.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:21]);
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:46]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
    }];
    UILabel * smsCloudPhoneAlertLab = [[UILabel alloc] init];
    smsCloudPhoneAlertLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#333333"];
    smsCloudPhoneAlertLab.text = @"请将手机号复制到注册平台填写，并发送验证码,发送成功后至【注册记录】查看验证码";
    smsCloudPhoneAlertLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:26] weight:UIFontWeightRegular];
    smsCloudPhoneAlertLab.numberOfLines = 2;
    [self addSubview:smsCloudPhoneAlertLab];
    [smsCloudPhoneAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(smsCloudContentView.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:21]);
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:45]));
        make.left.equalTo(smsCloudPhoneAlertImgView.mas_right).offset([SmsCloudHead SmsCloudScreewRatio:12]);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:72]));
    }];
    UIButton * smsCloudRecordBtn = [[UIButton alloc] init];
    [smsCloudRecordBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudCheckRecord"] forState:UIControlStateNormal];
    [smsCloudRecordBtn addTarget:self action:@selector(smsCloudRecord) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:smsCloudRecordBtn];
    [smsCloudRecordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:54]));
        make.centerX.equalTo(self);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:350]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:74]));
    }];
    UIButton * smsCloudRecordCloseBtn = [[UIButton alloc] init];
    [smsCloudRecordCloseBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudClose"] forState:UIControlStateNormal];
    [smsCloudRecordCloseBtn addTarget:self action:@selector(smsCloudRecordClose) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:smsCloudRecordCloseBtn];
    [smsCloudRecordCloseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:28]));
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:50]));
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:44]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:44]));
    }];
}
- (void)saveSmsCloud:(SmsCloudCityModel *)smsCloudCityMdl withProduct:(SmsCloudProductModel *)smsCloudProductMdl withPhone:(NSString *)phone{
    NSMutableDictionary *parmDic = [[NSMutableDictionary alloc] init];
    [parmDic setObject:smsCloudProductMdl.code forKey:@"productCode"];
    [parmDic setObject:smsCloudProductMdl.name forKey:@"productName"];
    [parmDic setObject:smsCloudCityMdl.info.code forKey:@"cityCode"];
    [parmDic setObject:smsCloudCityMdl.info.name forKey:@"cityName"];
    [parmDic setObject:phone forKey:@"phone"];
    [parmDic setObject:[self saveSmsCloudData] forKey:@"time"];
    [self.smsCloudRecords insertObject:parmDic atIndex:0];
    [[NSUserDefaults standardUserDefaults] setObject:_smsCloudRecords.copy forKey:@"SmsCloudRecord"];
}
- (NSString*)saveSmsCloudData{
    NSDateFormatter *smsCloudDataFormatter = [[NSDateFormatter alloc] init];
    [smsCloudDataFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [smsCloudDataFormatter stringFromDate:[NSDate date]];
    return dateString;
}
- (void)smsCloudPhoneCopy{
    NSString *smsCloudPhoneContent = self.smsCloudPhoneContentLab.text;
    if(smsCloudPhoneContent.length){
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = smsCloudPhoneContent;
        [SVProgressHUD showInfoWithStatus:@"复制成功"];
    }
}
- (void)smsCloudRecordClose{
    [self.smsCloudPhoneAlertPopUp dismiss];
}
- (void)smsCloudRecord{
    if(self.SmsCloudGoRecorBlock){
        self.SmsCloudGoRecorBlock();
        [self.smsCloudPhoneAlertPopUp dismiss];
    }
}
- (void)requestSmsPhone{
    __weak __typeof__(self) weakSelf = self;
    [SmsCloudDataHelper requestSmsCloudRedomPhone:self.smsCloudCityMdl.info.code withProductCode:self.smsCloudProductMdl.code scuessBlock:^(SmsCloudPhoneModel * _Nonnull smsCloudPhoneMdl) {
        weakSelf.smsCloudPhoneContentLab.text = [NSString stringWithFormat:@"+%@",smsCloudPhoneMdl.phoneNumber];
        [weakSelf saveSmsCloud:weakSelf.smsCloudCityMdl withProduct:weakSelf.smsCloudProductMdl withPhone:smsCloudPhoneMdl.phoneNumber];
    }];
}
-(void)setSmsCloudCityMdl:(SmsCloudCityModel *)smsCloudCityMdl{
    _smsCloudCityMdl = smsCloudCityMdl;
    if(_smsCloudCityMdl){
        self.smsCloudPlaContentLab.text = _smsCloudCityMdl.info.name;
    }
}
+ (void)showSmsCloudPhoneAlertPopUp:(UIView *)keyView withCity:(SmsCloudCityModel *)smsCloudCityMdl withProduct:(SmsCloudProductModel *)smsCloudProductMdl withComplaint:(void(^)(void))complaint{
    SmsCloudPhoneAlertPopUpView *smsCloudPhoneAlertPopUpView = [[SmsCloudPhoneAlertPopUpView alloc] init];
    zhPopupController *smsCloudPhoneAlertPopUp = [[zhPopupController alloc] initWithView:smsCloudPhoneAlertPopUpView size:CGSizeMake([SmsCloudHead SmsCloudScreewRatio:686], [SmsCloudHead SmsCloudScreewRatio:608])];
    smsCloudPhoneAlertPopUpView.SmsCloudGoRecorBlock = complaint;
    smsCloudPhoneAlertPopUpView.smsCloudPhoneAlertPopUp = smsCloudPhoneAlertPopUp;
    smsCloudPhoneAlertPopUpView.smsCloudCityMdl = smsCloudCityMdl;
    smsCloudPhoneAlertPopUpView.smsCloudProductMdl = smsCloudProductMdl;
    smsCloudPhoneAlertPopUp.presentationStyle = zhPopupSlideStyleTransform;
    smsCloudPhoneAlertPopUp.layoutType = zhPopupLayoutTypeCenter;
    smsCloudPhoneAlertPopUp.presentationTransformScale = 1.25;
    smsCloudPhoneAlertPopUp.dismissonTransformScale = 0.85;
    smsCloudPhoneAlertPopUp.maskAlpha = 0.6;
    [smsCloudPhoneAlertPopUp showInView:keyView completion:^{
        [smsCloudPhoneAlertPopUpView requestSmsPhone];
    }];
}
@end
